from peft import AutoPeftModelForCausalLM
path_to_adapter='/home/again/LLM/Qwen/output_qwen/'
new_model_directory='/home/again/LLM/Qwen-1_8B_chatpath'
model = AutoPeftModelForCausalLM.from_pretrained(
        path_to_adapter,
        device_map='auto',
        trust_remote_code=True
        ).eval()

merged_model = model.merge_and_unload()

merged_model.save_pretrained(new_model_directory, max_shard_size="2048MB",safe_serialization=True)

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(
        path_to_adapter,
        trust_remote_code=True,
        )

tokenizer.save_pretrained(new_model_directory)

